package image;


import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;

/**
 * @author hubert
 * @date 2018/5/14
 * @description ReadResolution 读取大图片的分辨率
 */
public class ReadResolution {
	/**
	 * 通过BufferedImage获取
	 * @param file 文件
	 * @return 图片的分辨率
	 * @throws IOException
	 */
	public static String getResolution1(File file) throws IOException {
		BufferedImage image = ImageIO.read(file);
		return image.getWidth() + "x" + image.getHeight();
	}

	/**
	 * 获取图片的分辨率
	 *
	 * @param path
	 * @return
	 */
	public static Dimension getImageDim(String path) {
		Dimension result = null;
		String suffix = getFileSuffix(path);
		//解码具有给定后缀的文件
		Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
		System.out.println(ImageIO.getImageReadersBySuffix(suffix));
		if (iter.hasNext()) {
			ImageReader reader = iter.next();
			try {
				ImageInputStream stream = new FileImageInputStream(new File(
						path));
				reader.setInput(stream);
				int width = reader.getWidth(reader.getMinIndex());
				int height = reader.getHeight(reader.getMinIndex());
				result = new Dimension(width, height);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				reader.dispose();
			}
		}
		System.out.println("getImageDim:" + result);
		return result;
	}

	/**
	 * 获得图片的后缀名
	 * @param path
	 * @return
	 */
	private static String getFileSuffix(final String path) {
		String result = null;
		if (path != null) {
			result = "";
			if (path.lastIndexOf('.') != -1) {
				result = path.substring(path.lastIndexOf('.'));
				if (result.startsWith(".")) {
					result = result.substring(1);
				}
			}
		}
		System.out.println("getFileSuffix:" + result);
		return result;
	}

	/**
	 * 截取Dimension对象获得分辨率
	 * @param path
	 *
	 * @return
	 */
	public static String getResolution2(String path) {
		String s = getImageDim(path).toString();
		s = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
		String w = s.substring(s.indexOf("=") + 1, s.indexOf(","));
		String h = s.substring(s.lastIndexOf("=") + 1);
		String result = w + " x " + h;
		System.out.println("getResolution:" + result);
		return result;
	}

	/**
	 * 测试
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String path = "d:\\abc.JPG";
		File file = new File(path);
		System.out.println("第1种方法使用的时间：");
		long s1 = System.currentTimeMillis();
		System.out.println(getResolution1(file));
		long l1 = System.currentTimeMillis();
		System.out.println((l1 - s1)+"ms");
		System.out.println("******************************");
		System.out.println("第2种方法使用的时间：");
		long s2 = System.currentTimeMillis();
		getResolution2(path);
		long l2 = System.currentTimeMillis();
		System.out.println((l2 - s2)+"ms");
	}
}
